package org.example.classtopia.Controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.classtopia.Model.dto.ResourceUploadDTO;
import org.example.classtopia.Service.ResourceService;
import org.example.classtopia.Utils.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "文件管理", description = "文件上传与下载接口")
@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private AliOSSUtils ossUtil;

    @Autowired
    private ResourceService resourceService;

    // 上传文件
    @Operation(summary = "上传文件", description = "上传文件到阿里云OSS")
    @PostMapping("/upload")
    public String upload(
            @Parameter(description = "要上传的文件", required = true)
            @RequestParam("file") MultipartFile file) throws Exception {
        return ossUtil.upload(file, "classtopia-file");
    }

    /**
     * 上传资源文件并保存资源信息
     */
    @Operation(summary = "上传资源", description = "上传资源文件到OSS并保存资源信息")
    @PostMapping("/uploadResource")
    public Map<String, Object> uploadResource(
            @Parameter(description = "上传用户ID", required = true) @RequestParam Long userId,
            @Parameter(description = "上传用户昵称", required = true) @RequestParam String userNickname,
            @Parameter(description = "上传用户头像") @RequestParam(required = false) String userAvatar,
            @Parameter(description = "资源标题", required = true) @RequestParam String title,
            @Parameter(description = "资源描述") @RequestParam(required = false) String description,
            @Parameter(description = "分类ID", required = true) @RequestParam Long categoryId,
            @Parameter(description = "要上传的文件", required = true) @RequestParam("file") MultipartFile file
    ) throws Exception {
        // 打印所有前端传入参数
        System.out.println("[上传资源] userId=" + userId + ", title=" + title + ", description=" + description + ", categoryId=" + categoryId + ", fileName=" + (file != null ? file.getOriginalFilename() : null));
        ResourceUploadDTO dto = new ResourceUploadDTO();
        dto.setUserId(userId);
        dto.setTitle(title);
        dto.setDescription(description);
        dto.setCategoryId(categoryId);
        resourceService.uploadResource(dto, file);
        Map<String, Object> result = new HashMap<>();
        result.put("message", "success");
        result.put("code", 200);
        return result;
    }

    /**
     * 下载资源文件
     */
    @Operation(summary = "下载资源", description = "根据资源ID和用户ID下载资源")
    @GetMapping("/download")
    public Map<String, Object> downloadResource(
            @Parameter(description = "资源ID", required = true) @RequestParam Long resourceId,
            @Parameter(description = "下载用户ID", required = true) @RequestParam Long userId
    ) {
        Map<String, Object> result = new HashMap<>();
        try {
            var resource = resourceService.downloadResource(resourceId, userId);
            if (resource == null) {
                result.put("message", "资源不存在或已被删除");
                result.put("code", 404);
                return result;
            }
            result.put("fileUrl", resource.getFileUrl());
            result.put("fileName", resource.getTitle());
            result.put("message", "success");
            result.put("code", 200);
        } catch (Exception e) {
            result.put("message", e.getMessage());
            result.put("code", 500);
        }
        return result;
    }

    /**
     * 上传图片到阿里云OSS并返回图片URL
     */
    @Operation(summary = "上传图片", description = "上传图片到OSS并返回图片URL")
    @PostMapping("/uploadImage")
    public Map<String, Object> uploadImage(
            @Parameter(description = "要上传的图片文件", required = true) @RequestParam("file") MultipartFile file
    ) throws Exception {
        Map<String, Object> result = new HashMap<>();
        // 打印前端传入参数
        System.out.println("[上传图片] fileName=" + (file != null ? file.getOriginalFilename() : null));
        try {
            // 调用 Service 层处理上传
            String url = resourceService.uploadImage(file);
            result.put("url", url);
            result.put("message", "success");
            result.put("code", 200);
        } catch (Exception e) {
            result.put("message", "图片上传失败: " + e.getMessage());
            result.put("code", 500);
        }
        return result;
    }
}
